package cn.hzw.supermarket.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import cn.hzw.supermarket.R;
import cn.hzw.supermarket.activity.BreadActivity;
import cn.hzw.supermarket.activity.DrinksActivity;
import cn.hzw.supermarket.activity.FruitActivity;
import cn.hzw.supermarket.activity.GrainOilActivity;
import cn.hzw.supermarket.activity.LoginActivity;
import cn.hzw.supermarket.activity.SearchActivity;
import cn.hzw.supermarket.activity.SnackActivity;
import cn.hzw.supermarket.activity.StoreActivity;
import cn.hzw.supermarket.databinding.FragmentHomeBinding;
import cn.hzw.supermarket.databinding.FragmentMeBinding;
import cn.hzw.supermarket.domain.User;
import cn.hzw.supermarket.model.ResultInfo;
import okhttp3.OkHttpClient;


public class HomeFragment extends Fragment {
    private FragmentHomeBinding _binding;
    User user = new User();
    final OkHttpClient client = new OkHttpClient();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        _binding = FragmentHomeBinding.inflate(inflater,container,false);
        return _binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        user.setStatus("0");
        if (!readLoginStatus()) {
            if (LoginActivity.user!=null) {
                Log.d("TAG1", "initView: dfdffd" + LoginActivity.user.getStatus());
            }
            _binding.bread.setOnClickListener(view -> {
                Toast.makeText(getContext(), "您还未登录，请先登录", Toast.LENGTH_SHORT).show();
            });
            _binding.snack.setOnClickListener(view -> {
                Toast.makeText(getContext(), "您还未登录，请先登录", Toast.LENGTH_SHORT).show();
            });
            _binding.drinks.setOnClickListener(view -> {
                Toast.makeText(getContext(), "您还未登录，请先登录", Toast.LENGTH_SHORT).show();
            });
            _binding.fruit.setOnClickListener(view -> {
                Toast.makeText(getContext(), "您还未登录，请先登录", Toast.LENGTH_SHORT).show();
            });
            _binding.store.setOnClickListener(view -> {
                Toast.makeText(getContext(), "您还未登录，请先登录", Toast.LENGTH_SHORT).show();
            });
            _binding.grainOil.setOnClickListener(view -> {
                Toast.makeText(getContext(), "您还未登录，请先登录", Toast.LENGTH_SHORT).show();
            });
        }else {
            Log.d("TAG1", "initView: dfdffd"+LoginActivity.user.getStatus());
                _binding.bread.setOnClickListener(view -> {
                    Log.d("TAG123", "initView: "+LoginActivity.user.getStatus());
                    if (LoginActivity.user.getStatus()==null){
                        Toast.makeText(getContext(), "您还未登录，请先登录", Toast.LENGTH_SHORT).show();
                    }else {
                        Intent intent = new Intent(getContext(), BreadActivity.class);
                        startActivity(intent);
                    }
                });
                _binding.snack.setOnClickListener(view -> {
                    if (LoginActivity.user.getStatus()==null) {
                        Toast.makeText(getContext(), "您还未登录，请先登录", Toast.LENGTH_SHORT).show();
                    }else {
                        Intent intent = new Intent(getContext(), SnackActivity.class);
                        startActivity(intent);
                    }
                });
                _binding.drinks.setOnClickListener(view -> {
                    if (LoginActivity.user.getStatus()==null) {
                        Toast.makeText(getContext(), "您还未登录，请先登录", Toast.LENGTH_SHORT).show();
                    }else {
                        Intent intent = new Intent(getContext(), DrinksActivity.class);
                        startActivity(intent);
                    }
                });
                _binding.fruit.setOnClickListener(view -> {
                    if (LoginActivity.user.getStatus()==null) {
                        Toast.makeText(getContext(), "您还未登录，请先登录", Toast.LENGTH_SHORT).show();
                    }else {
                        Intent intent = new Intent(getContext(), FruitActivity.class);
                        startActivity(intent);
                    }
                });
                _binding.store.setOnClickListener(view -> {
                    if (LoginActivity.user.getStatus()==null) {
                        Toast.makeText(getContext(), "您还未登录，请先登录", Toast.LENGTH_SHORT).show();
                    }else {
                        Intent intent = new Intent(getContext(), StoreActivity.class);
                        startActivity(intent);
                    }
                });
                _binding.grainOil.setOnClickListener(view -> {
                    if (LoginActivity.user.getStatus()==null) {
                        Toast.makeText(getContext(), "您还未登录，请先登录", Toast.LENGTH_SHORT).show();
                    }else {
                        Intent intent = new Intent(getContext(), GrainOilActivity.class);
                        startActivity(intent);
                    }
                });
            _binding.searchGoods.setOnClickListener(view -> {
                if (LoginActivity.user.getStatus()==null) {
                    Toast.makeText(getContext(), "您还未登录，请先登录", Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent = new Intent(getContext(), SearchActivity.class);
                    startActivity(intent);
                }
            });
            }

    }

    private boolean readLoginStatus() {

        ResultInfo resultInfo = new ResultInfo();

        Log.d("TAG", "readLoginStatus:112d " + resultInfo.getUser());
        if (LoginActivity.user == null && user.getStatus().equals("0")) {
            user.setStatus("0");
            return false;
        } else {
            user.setStatus("1");
            return true;
        }
    }
}



